<?php
    class URL
    {
        public static $sHost = 'http://icenter.com';
        public static $sDocumentRoot = '';
        public static $sPath = '';
        public static function getLink($sPath = '')
        {
            if(strpos($sPath,"http") !== false)
            {
                $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
                preg_match($reg_exUrl, $sPath, $url);
                return $url[0];
            }
            if(URL::$sDocumentRoot != '')
            {
                $sLink = URL::$sHost."/".URL::$sDocumentRoot."/".$sPath;
            }
            else
            {
                $sLink = URL::$sHost."/".$sPath;
            }
            return $sLink;
        }

        public static function getShortLink($sController = '', $sAction = '', $sId = '')
        {
            $sLink = "~$sController/$sAction/$sId";
            return $sLink;
        }

        public static function getParam($sParam = '', $bString = false,$bFullCondition = false)
        {
            if(isset($_GET[$sParam]))
            {
                if(!$bString)
                {
                    if($_GET[$sParam] == '')
                    {
                        return false;
                    }
                }
                if($bFullCondition)
                {
                    return ($sParam.'='.$_GET[$sParam]);
                }
                else
                {
                    return $_GET[$sParam];
                }
            }
            else
            {
                if($bString)
                {
                    return '';
                }
                else
                {
                    return false;
                }
            }
        }

        public static function parse_url($url)
        {
            $encodedUrl = preg_replace('%[^:/?#&=\.]+%usDe', 'urlencode(\'$0\')', $url);
            $components = parse_url($encodedUrl);
            foreach ($components as &$component)
                $component = urldecode($component);
            return $components;
        }

        public static function convertLink($sLink)
        {
            $sLink = html_entity_decode($sLink);
            $info = parse_url($sLink);
            $safePath = implode('/', array_map('rawurlencode', explode('/', $info['path'])));
            $newUrl = sprintf('%s://%s%s', $info['scheme'], $info['host'], $safePath);
            return $newUrl;
        }
    }

    class Text
    {
        public static function hightLight($sString = '',$sKeyWord = '')
        {
            return str_ireplace($sKeyWord, '<span class="hight_light">'.$sKeyWord.'</span>',$sString);
        }

        public static function convert_vi_to_en($str) 
        {
            $str = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", 'a', $str);
            $str = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", 'e', $str);
            $str = preg_replace("/(ì|í|ị|ỉ|ĩ)/", 'i', $str);
            $str = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", 'o', $str);
            $str = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", 'u', $str);
            $str = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", 'y', $str);
            $str = preg_replace("/(đ)/", 'd', $str);
            $str = preg_replace("/(À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ)/", 'A', $str);
            $str = preg_replace("/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/", 'E', $str);
            $str = preg_replace("/(Ì|Í|Ị|Ỉ|Ĩ)/", 'I', $str);
            $str = preg_replace("/(Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ)/", 'O', $str);
            $str = preg_replace("/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", 'U', $str);
            $str = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", 'Y', $str);
            $str = preg_replace("/(Đ)/", 'D', $str);
            return $str;
        }

        public static function clean($string) {
            // Replaces all spaces with hyphens.
            $string = str_replace(' ', '-', $string); 
            // Removes special chars.
            $string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); 
            // Replaces multiple hyphens with single one.
            return preg_replace('/-+/', '-', $string); 
        }

    }
?>
